package de.cube3d.blitwit.test
{
	import de.cube3d.blitwit.test.cases.Test1M;
	import de.cube3d.blitwit.test.cases.TestMovement;
	import de.cube3d.blitwit.test.cases.TestScaling;
	import flash.display.StageAlign;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.events.KeyboardEvent;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * main class for tests for the blitwit code
	 */
	public class Main extends Sprite 
	{
		private var _tests:Vector.<Class> = Vector.<Class>([
			Test1M, TestMovement, TestScaling
		]);
		
		private var _test:Test;
		
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		
		
		public static var _sptest:Sprite = new Sprite();
		public static function rect(x:int, y:int, w:int, h:int):void {
			_sptest.graphics.beginFill(0xffff00);
			Main._sptest.graphics.drawRect(x, y, w, h);
			Main._sptest.graphics.drawRect(x+2, y+2, w-4, h-4);
			Main._sptest.graphics.endFill();
		}
		private var _tf:TextField = new TextField();
		private function init(e:Event = null):void 
		{
			stage.scaleMode = "noScale";
			stage.frameRate = 120;
			stage.align = StageAlign.TOP_LEFT;
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			_test = new TestMovement();
			
			_tf.textColor = 0xffffff;
			_tf.width = 400;
			_tf.height = 300;
			_tf.multiline = true;
			_tf.mouseEnabled = false;
			addChild(_tf);
			addChild(_sptest);
			
			// entry point
			stage.addEventListener(Event.ENTER_FRAME, render);
			stage.addEventListener(Event.RESIZE, onResize);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
			stage.addEventListener(Event.DEACTIVATE, clearKeys);	
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			//scene.updateImage();
		}
		
		private var _rt:int;
		private function render (e:Event):void {
			_sptest.graphics.clear();
			_test.render(graphics, stage.stageWidth, stage.stageHeight);
			
			_tf.text = _test.getStats();
		}
		private function onResize (e:Event):void {
			//scene.setDimension(stage.stageWidth, stage.stageHeight);
			//scene.updateImage();
		}
		
		private function keyPressed(event:KeyboardEvent):void {
			_test.keyPressed(event);
		}
		private function keyReleased(event:KeyboardEvent):void {
			_test.keyReleased(event);
		}
		private function clearKeys(event:Event):void {
			_test.clearKeys(event);
		}
		
		private function mouseMoved (e: MouseEvent):void {
			_test.mouseMoved(e.localX, e.localY);
		}
		
		private function mouseUp (e: MouseEvent): void {
			_test.mouseUp(e.localX, e.localY);
		}
		
		private function mouseDown (e: MouseEvent): void {
			_test.mouseDown(e.localX, e.localY);
		}
	}
	
}